package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.*;
import com.hxgz.steel.dto.HomeProductDto;
import com.hxgz.steel.dto.ProductCommentsDto;
import com.hxgz.steel.dto.ProductStoreDto;
import com.hxgz.steel.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(tags = "ProductController",description = "前台-商品相关-商品详情")
@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;


    @RequestMapping(value = "productInfo.do",method = RequestMethod.POST)
    @ApiOperation(value = "商品详情")
    public ServerResponse<Product> productInfo(@ApiParam(value = "商品id",required = true)@RequestParam Long id){

        return productService.productInfo(id);
    }

    @ApiOperation(value = "商品详情-根据型号获取价格")
    @RequestMapping(value = "getPriceByKind",method = RequestMethod.POST)
    public ServerResponse getPriceByKind(@ApiParam("商品id")@RequestParam Long productId,
                                                     @ApiParam("型号")@RequestParam(value = "model") String model,
                                                     @ApiParam("颜色")@RequestParam(value = "color") String color,
                                                     @ApiParam("数量")@RequestParam(value = "count",defaultValue = "1") Integer count){

        return productService.getPriceByKind( productId, model, color, count);
    }

    @ApiOperation("商品详情-商品所属店铺")
    @RequestMapping(value = "getStoreByProduct",method = RequestMethod.POST)
    public ServerResponse<ProductStoreDto> getStoreByProduct(@ApiParam(value = "商家id",required = true)@RequestParam Long merchantId){

        return productService.getStoreByProduct(merchantId);
    }

    @ApiOperation("商品详情-立即购买")
    @RequestMapping(value = "buyNow",method = RequestMethod.POST)
    public ServerResponse<Address> buyNow(HttpServletRequest httpServletRequest){
        User user= Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return productService.buyNow(user.getId());
    }
    @ApiOperation("商品详情-商品参数")
    @RequestMapping(value = "productParams",method = RequestMethod.POST)
    public ServerResponse<List<ProductParameter>> productParams(@ApiParam(value = "商品id",required = true)@RequestParam Long id){

        return productService.productParams(id);
    }
    @ApiOperation("商品详情-商品评论")
    @RequestMapping(value = "productComments",method = RequestMethod.POST)
    public ServerResponse<PageInfo<List<ProductCommentsDto>>> productComments(@ApiParam(value = "商品id",required = true)@RequestParam Long id,
                                                                              Page page){

        return productService.productComments(id,page);
    }

    @ApiOperation("商铺下商品列表")
    @RequestMapping(value = "storeProdectList.do",method = RequestMethod.POST)
    public ServerResponse<PageInfo<List<HomeProductDto>>> storeProdectList(@ApiParam("店铺id")@RequestParam Long id, Page page){

        return productService.storeProdectList(id,page);
    }
    @ApiOperation("获取商品类型")
    @RequestMapping(value = "getProductType.do",method = RequestMethod.POST)
    public ServerResponse getProductType(Long id,HttpServletRequest request){
        User user= Const.checkUserLogin(request);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return productService.getProductType(id);
    }
}
